/******************************************************************************
 * Copyright (c) 2006, Intalio Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intalio Inc. - initial API and implementation
 *******************************************************************************/

/** 
 * 
 * Date         	Author             Changes 
 * 1 Sep 2006   	MPeleshchyshyn  	Created 
 */
package ms.unicorn.ui.figures;

import ms.unicorn.ui.borders.OvalSchemeBorder;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.IOvalAnchorableFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.SlidableOvalAnchor;

/**
 * Oval node figure for event activities. Sets preferred size equal to size
 * specified in setBounds() method if current preferred size is
 * <code>null</code>. This allows to prevent figure resizing as user types
 * label text.
 * 
 * @author MPeleshchyshyn
 * @author <a href="http://www.intalio.com">&copy; Intalio, Inc.</a>
 */
public class EntityOvalFigure extends NodeFigure implements IOvalAnchorableFigure {
    
	private boolean rounded = false;
	protected OvalSchemeBorder border;
	private Theme theme;
	
	public EntityOvalFigure() {
		theme = new Theme();
		theme.setFigure(this);
		border = new OvalSchemeBorder();
		setBorder(border);
		border.setTheme(theme);
	}
	

    /**
     * Sets the bounds of this Figure to the Rectangle <i>rect</i> and stores
     * these bounds as figure's preferred size when this method is called on
     * object for the forst time. Note that <i>rect</i> is compared to the
     * Figure's current bounds to determine what needs to be repainted and/or
     * exposed and if validation is required. Since {@link #getBounds()} may
     * return the current bounds by reference, it is not safe to modify that
     * Rectangle and then call setBounds() after making modifications. The
     * figure would assume that the bounds are unchanged, and no layout or paint
     * would occur. For proper behavior, always use a copy.
     * 
     * @param rect
     *            The new bounds
     */
    @Override
    public void setBounds(Rectangle rect) {
    	if (rounded) {
	        Rectangle r = rect.getCopy();
	        r.height = Math.min(r.height, r.width);
	        r.width = r.height;
	        super.setBounds(r);
    	}
    	else
    		super.setBounds(rect);
    }
    
    @Override
    protected ConnectionAnchor createDefaultAnchor() {
    	return new SliableOvalAnchorEx(this);
    }
    
    @Override
    protected ConnectionAnchor createAnchor(PrecisionPoint p) {
    	if (p==null)
			return createDefaultAnchor();
		return new SliableOvalAnchorEx(this, p);
    }
    
    @Override
    protected void paintFigure(Graphics graphics) {
        paintShadow(graphics);
        // otherwise stay transparent
    }

    protected void paintShadow(Graphics graphics) {
        super.paintBorder(graphics);
    }
    
    @Override
    protected void paintBorder(Graphics graphics) {
        //do nothing. we paint the border before the rest of the figure as it is where we paint the shadow.
    }

    @Override
    public IFigure getToolTip() {
       /* if (getChildren().iterator().next() instanceof ActivityEditPart.ActivityFigure) {
            return ((ActivityEditPart.ActivityFigure) getChildren().iterator().
                    next()).getFigureActivityNameFigure().getToolTip();
        } else {
            if (getChildren().iterator().next() instanceof Activity2EditPart.ActivityFigure) {
                return ((Activity2EditPart.ActivityFigure) getChildren().iterator().
                        next()).getFigureActivityNameFigure().getToolTip();
            }
        }*/
        return null;
    }
    
    private static final Dimension MIN_SIZE = new Dimension(6, 6);

    @Override
    public Dimension getMinimumSize(int hint, int hint2) {
        Dimension d = super.getMinimumSize(hint, hint2);
        if (d.height < MIN_SIZE.height) {
            return MIN_SIZE;
        } else {
            return d;
        }
    }

	@Override
	public Rectangle getOvalBounds() {
		return getBounds();
	}

	public Theme getTheme() {
		return theme;
	}

	public void setTheme(Theme theme) {
		this.theme = theme;
	}
    
    
}
